<template>
	<div>
		<jk-table
			ref="frontGroupSalaryMonthReportTable"
			border
			show-footer
			:span-method="mergeColMethod"
			:footer-method="footerMethod"
			:data="tableData"
			:height="height"
			:loading="loading"
		>
			<vxe-column title="时间（小时）" align="left" field="belongTime" width="110">
				<template slot-scope="scope">
					<span>{{ scope.row.belongTime + 1 }}</span>
				</template>
			</vxe-column>
			<vxe-colgroup v-if="machineCode === 'YX001' || machineCode === ''" title="异纤机1号" align="center">
				<vxe-column title="可见（喷阀动作次数3（彩色/h））" align="right" field="YX001KeJian" width="260">
				</vxe-column>
				<vxe-column title="紫外（喷阀动作次数4（彩色/h））" align="right" field="YX001ZiWai" width="260">
				</vxe-column>
			</vxe-colgroup>
			<vxe-colgroup v-if="machineCode === 'YX002' || machineCode === ''" title="异纤机2号" align="center">
				<vxe-column title="可见（喷阀动作次数3（彩色/h））" align="right" field="YX002KeJian" width="260">
				</vxe-column>
				<vxe-column title="紫外（喷阀动作次数4（彩色/h））" align="right" field="YX002ZiWai" width="260">
				</vxe-column>
			</vxe-colgroup>
		</jk-table>
	</div>
</template>

<script>
    import XEUtils from 'xe-utils';

    export default {
        props: {
            tableData: {
                type: Array,
                default() {
                    return [];
                }
            },
            searchParams: {
                type: Object,
                default() {
                    return {};
                }
            },
            height: {
                type: [String, Number],
                default: 0
            },
            machineCode: {
                type: [String, Number],
                default: ''
            },
            loading: {
                type: Boolean,
                default: false
            }
        },
        data() {
            return {
                noCalKey: ['YX001KeJian', 'YX001ZiWai', 'YX002KeJian', 'YX002ZiWai']
            };
        },
        methods: {
            mergeColMethod({ row, _rowIndex, column, visibleData }) {
                const prevRow = visibleData[_rowIndex - 1];
                let nextRow = visibleData[_rowIndex + 1];
                if (column.property === 'belongDate') {
                    if (prevRow && prevRow.belongDate === row.belongDate) {
                        return { rowspan: 0, colspan: 0 };
                    } else {
                        let countRowspan = 1;
                        while (nextRow && nextRow.belongDate === row.belongDate) {
                            nextRow = visibleData[++countRowspan + _rowIndex];
                        }
                        if (countRowspan > 1) {
                            return { rowspan: countRowspan, colspan: 1 };
                        }
                    }
                }
            },
            footerMethod({ columns, data }) {
                return [
                    columns.map((column, columnIndex) => {
                        if (columnIndex === 0) {
                            return `合计`;
                        }
                        if (this.noCalKey.includes(column.property)) {
                            return XEUtils.sum(data, column.property);
                        }
                        return null;
                    })
                ];
            }
        }
    };
</script>
